package curd;


import curd.Cjempleado;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.transaction.Transaction;
import org.hibernate.Session;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ingresar.java
 *
 * Created on 4/07/2011, 06:04:03 PM
 */
/**
 *
 * @author Javier
 */
public class Ingresar extends javax.swing.JFrame {

    /** Creates new form Ingresar */
    public Ingresar() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        l_nombre = new javax.swing.JLabel();
        l_apellido = new javax.swing.JLabel();
        l_fechanac = new javax.swing.JLabel();
        l_direccion = new javax.swing.JLabel();
        l_estado = new javax.swing.JLabel();
        l_puesto = new javax.swing.JLabel();
        l_tipo_contrato = new javax.swing.JLabel();
        e_nombre = new javax.swing.JTextField();
        e_apellido = new javax.swing.JTextField();
        e_direccion = new javax.swing.JTextField();
        e_puesto = new javax.swing.JTextField();
        rb_soltero = new javax.swing.JRadioButton();
        rb_casado = new javax.swing.JRadioButton();
        rb_tipo1 = new javax.swing.JRadioButton();
        rb_tipo2 = new javax.swing.JRadioButton();
        cb_dia = new javax.swing.JComboBox();
        cb_mes = new javax.swing.JComboBox();
        cb_año = new javax.swing.JComboBox();
        boton_cancel = new javax.swing.JButton();
        boton_aceptar = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(curd.CURDApp.class).getContext().getResourceMap(Ingresar.class);
        l_nombre.setText(resourceMap.getString("l_nombre.text")); // NOI18N
        l_nombre.setName("l_nombre"); // NOI18N

        l_apellido.setText(resourceMap.getString("l_apellido.text")); // NOI18N
        l_apellido.setName("l_apellido"); // NOI18N

        l_fechanac.setText(resourceMap.getString("l_fechanac.text")); // NOI18N
        l_fechanac.setName("l_fechanac"); // NOI18N

        l_direccion.setText(resourceMap.getString("l_direccion.text")); // NOI18N
        l_direccion.setName("l_direccion"); // NOI18N

        l_estado.setText(resourceMap.getString("l_estado.text")); // NOI18N
        l_estado.setName("l_estado"); // NOI18N

        l_puesto.setText(resourceMap.getString("l_puesto.text")); // NOI18N
        l_puesto.setName("l_puesto"); // NOI18N

        l_tipo_contrato.setText(resourceMap.getString("l_tipo_contrato.text")); // NOI18N
        l_tipo_contrato.setName("l_tipo_contrato"); // NOI18N

        e_nombre.setText(resourceMap.getString("e_nombre.text")); // NOI18N
        e_nombre.setName("e_nombre"); // NOI18N

        e_apellido.setText(resourceMap.getString("e_apellido.text")); // NOI18N
        e_apellido.setName("e_apellido"); // NOI18N

        e_direccion.setText(resourceMap.getString("e_direccion.text")); // NOI18N
        e_direccion.setName("e_direccion"); // NOI18N

        e_puesto.setText(resourceMap.getString("e_puesto.text")); // NOI18N
        e_puesto.setName("e_puesto"); // NOI18N

        rb_soltero.setText(resourceMap.getString("rb_soltero.text")); // NOI18N
        rb_soltero.setName("rb_soltero"); // NOI18N

        rb_casado.setText(resourceMap.getString("rb_casado.text")); // NOI18N
        rb_casado.setName("rb_casado"); // NOI18N

        rb_tipo1.setText(resourceMap.getString("rb_tipo1.text")); // NOI18N
        rb_tipo1.setName("rb_tipo1"); // NOI18N

        rb_tipo2.setText(resourceMap.getString("rb_tipo2.text")); // NOI18N
        rb_tipo2.setName("rb_tipo2"); // NOI18N

        cb_dia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        cb_dia.setName("cb_dia"); // NOI18N

        cb_mes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));
        cb_mes.setName("cb_mes"); // NOI18N

        cb_año.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011" }));
        cb_año.setName("cb_año"); // NOI18N

        boton_cancel.setText(resourceMap.getString("boton_cancel.text")); // NOI18N
        boton_cancel.setName("boton_cancel"); // NOI18N

        boton_aceptar.setText(resourceMap.getString("boton_aceptar.text")); // NOI18N
        boton_aceptar.setName("boton_aceptar"); // NOI18N
        boton_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_aceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(l_nombre)
                    .addComponent(l_apellido)
                    .addComponent(l_fechanac)
                    .addComponent(l_direccion)
                    .addComponent(l_estado)
                    .addComponent(l_puesto)
                    .addComponent(l_tipo_contrato))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(rb_tipo1)
                            .addGap(69, 69, 69)
                            .addComponent(rb_tipo2))
                        .addComponent(e_nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                        .addComponent(e_apellido, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                        .addComponent(e_direccion, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                        .addComponent(e_puesto, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(rb_soltero)
                            .addGap(65, 65, 65)
                            .addComponent(rb_casado))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(boton_cancel)
                            .addGap(18, 18, 18)
                            .addComponent(boton_aceptar)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cb_dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cb_mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cb_año, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)))
                .addGap(59, 59, 59))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_nombre)
                    .addComponent(e_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_apellido)
                    .addComponent(e_apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_fechanac)
                    .addComponent(cb_dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_año, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_direccion)
                    .addComponent(e_direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_estado)
                    .addComponent(rb_soltero)
                    .addComponent(rb_casado))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_puesto)
                    .addComponent(e_puesto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l_tipo_contrato)
                    .addComponent(rb_tipo1)
                    .addComponent(rb_tipo2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton_aceptar)
                    .addComponent(boton_cancel))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boton_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_aceptarActionPerformed
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        try{
            //Transaction trans = (Transaction) session.beginTransaction();
            org.hibernate.Transaction trans = session.beginTransaction();
            Cjempleado empleado = new Cjempleado();
            empleado.setApellidos(e_apellido.getText());
            empleado.setNombres(e_nombre.getText());
            empleado.setDireccion(e_direccion.getText());
            empleado.setPuesto(e_puesto.getText());
            if(rb_soltero.isSelected() == true)
            {
                    empleado.setEstadoCivil(false);
                    
            } 
            
            else
            {
                empleado.setEstadoCivil(true);
                
            }
            SimpleDateFormat sdf= new SimpleDateFormat("dd-MM-yyyy"); 
            Date d = null;
            String fecha = cb_dia.getSelectedItem().toString() +"-" + cb_mes.getSelectedItem().toString() +"-" + cb_año.getSelectedItem().toString();
            JOptionPane.showMessageDialog(null, fecha);
            try {
                d = sdf.parse(fecha);
                empleado.setFechaNac(d);
                
            } catch (ParseException ex) {
//                Logger.getLogger(Insertar.class.getName()).log(Level.SEVERE, null, ex);
                
            }
            byte y  = 0;
            if (rb_tipo1.isSelected()==true)
               empleado.setContratoTipo(y);
            else  
            {
                y = (byte) (y+1);
                empleado.setContratoTipo(y);
            }
            //empleado.setTelCasa(e_telefono.getText());
            //empleado.setGradoAcademico("bachiller");
            //empleado.setIdEmpleado(1);
            session.save(empleado);
            JOptionPane.showMessageDialog(null, "empleado guardado con éxito");
            trans.commit();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null, "no se pudo");
        }
        finally 
        {
            session.close();
        }
    }//GEN-LAST:event_boton_aceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Ingresar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_aceptar;
    private javax.swing.JButton boton_cancel;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cb_año;
    private javax.swing.JComboBox cb_dia;
    private javax.swing.JComboBox cb_mes;
    private javax.swing.JTextField e_apellido;
    private javax.swing.JTextField e_direccion;
    private javax.swing.JTextField e_nombre;
    private javax.swing.JTextField e_puesto;
    private javax.swing.JLabel l_apellido;
    private javax.swing.JLabel l_direccion;
    private javax.swing.JLabel l_estado;
    private javax.swing.JLabel l_fechanac;
    private javax.swing.JLabel l_nombre;
    private javax.swing.JLabel l_puesto;
    private javax.swing.JLabel l_tipo_contrato;
    private javax.swing.JRadioButton rb_casado;
    private javax.swing.JRadioButton rb_soltero;
    private javax.swing.JRadioButton rb_tipo1;
    private javax.swing.JRadioButton rb_tipo2;
    // End of variables declaration//GEN-END:variables
}
